package com.jade.explorer.ui.exp;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.jade.explorer.dirscan.FileSystem;
import com.jade.explorer.ui.SWTResourceManager;
import com.jade.explorer.views.JadeSharedImages;

import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.jface.viewers.ListViewer;

public class SingleFileExportDialog extends Dialog {

	private String _path;
	private String _expPath = "";
	private boolean _result = false;
	private Button _okButton;
	private boolean _over = false;
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SingleFileExportDialog(Shell parentShell, String path) {
		super(parentShell);
		_path = path;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);

		//---------------------------
		Composite titleFrame = new Composite(container, SWT.NONE);
		titleFrame.setBounds(11, 13, 64, 64);
		titleFrame.setBounds(0, 0, 444, 44);
		titleFrame.setBackgroundImage(JadeSharedImages.getMenuTitleImg());
		titleFrame.setFont(SWTResourceManager.getFont("Rockwell", 14, SWT.BOLD));
		//----------------------------
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setBounds(0, 44, 444, 98);
		
		final Text text = new Text(composite, SWT.BORDER);
		text.setEditable(false);
		text.setBounds(10, 43, 359, 21);
		
		Button pathButton = new Button(composite, SWT.NONE);
		pathButton.setBounds(382, 43, 52, 21);
		pathButton.setText("...");
		final Shell shell = super.getShell();
		pathButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				DirectoryDialog dialog = new DirectoryDialog(shell);
				String importPath = dialog.open(); 
				
				if (importPath != null){
						text.setText(importPath);
						_expPath = importPath;
						_okButton.setEnabled(true);
					}
				}
			
		});
		
		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setBounds(142, 22, 161, 15);
		lblNewLabel.setText("Select folder to export file:");
		titleFrame.addPaintListener(new PaintListener() { 
			public void paintControl(PaintEvent e) { 
				e.gc.drawString("Export File", 10, 10, true); 
				} 
		}); 
		
		
		Button overWriteCheckButton = new Button(composite, SWT.CHECK);
		overWriteCheckButton.setBounds(10, 70, 170, 16);
		overWriteCheckButton.setText("Overwrite existing files");
		overWriteCheckButton.setSelection(false);
		overWriteCheckButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				if (_over) _over = false;
				else _over = true;
				
			}
		});

		
		
		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		final Shell shell = super.getShell();
		_okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		_okButton.setEnabled(false);
		_okButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				if (_expPath != ""){
					
					if (_over)
						_result = FileSystem.copyFileToWithOverwrite(_path, _expPath);
					else 
						_result = FileSystem.copyFileToWithNoOverwrite(_path, _expPath);
					close();
				}
				else {
					MessageDialog.openError(shell, "Error", "Choose directory for export");
					open();
				}
					
				
			}
		});
		
		Button cancelButton = createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
		cancelButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				_result = false;
				close();
			}
		});
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 220);
	}
	
	public boolean getResult(){
		return _result;
	}
	
}
